package com.quyunshuo.module.home.room

import androidx.room.TypeConverter
import com.quyunshuo.module.home.room.entity.PriorityLevel
import com.quyunshuo.module.home.room.entity.TodoCategory
import java.util.Date

class Converters {
    @TypeConverter
    fun fromTimestamp(value: Long?): Date? {
        return value?.let { Date(it) }
    }

    @TypeConverter
    fun dateToTimestamp(date: Date?): Long? {
        return date?.time
    }
}

class TagListConverter {
    @TypeConverter
    fun fromString(value: String?): List<String> {
        return value?.split(",")?.filter { it.isNotBlank() } ?: emptyList()
    }

    @TypeConverter
    fun toString(list: List<String>?): String {
        return list?.joinToString(",") ?: ""
    }
}

class CategoryConverter {
    @TypeConverter
    fun toCategory(value: String) = enumValueOf<TodoCategory>(value)

    @TypeConverter
    fun fromCategory(category: TodoCategory) = category.name
}

class PriorityConverter {
    @TypeConverter
    fun toPriority(value: String) = enumValueOf<PriorityLevel>(value)

    @TypeConverter
    fun fromPriority(priority: PriorityLevel) = priority.name
}